package com.ruoyi.web.controller.gd;

import java.util.List;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;

import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.core.validate.QueryGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.gd.invoice.domain.vo.ZInvoiceVo;
import com.ruoyi.gd.invoice.domain.bo.ZInvoiceBo;
import com.ruoyi.gd.invoice.service.IZInvoiceService;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 发票
 *
 * @author ruoyi
 * @date 2024-07-24
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/gd/invoice")
public class ZInvoiceController extends BaseController {

    private final IZInvoiceService iZInvoiceService;

    /**
     * 解析发票
     */
    @GetMapping("/jxFj/{id}")
    public R<Integer> jxFj(@PathVariable("id") String id) {
        int row = iZInvoiceService.jxFj(id);
        return R.ok(row);
    }

    /**
     * 查询发票列表
     */
    @SaCheckPermission("gd:invoice:list")
    @GetMapping("/list")
    public TableDataInfo<ZInvoiceVo> list(ZInvoiceBo bo, PageQuery pageQuery) {
        return iZInvoiceService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出发票列表
     */
    @SaCheckPermission("gd:invoice:export")
    @Log(title = "发票", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(ZInvoiceBo bo, HttpServletResponse response) {
        List<ZInvoiceVo> list = iZInvoiceService.queryList(bo);
        ExcelUtil.exportExcel(list, "发票", ZInvoiceVo.class, response);
    }

    /**
     * 获取发票详细信息
     *
     * @param number 主键
     */
    @SaCheckPermission("gd:invoice:query")
    @GetMapping("/{number}")
    public R<ZInvoiceVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable String number) {
        return R.ok(iZInvoiceService.queryById(number));
    }

    /**
     * 新增发票
     */
    @SaCheckPermission("gd:invoice:add")
    @Log(title = "发票", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody ZInvoiceBo bo) {
        return toAjax(iZInvoiceService.insertByBo(bo));
    }

    /**
     * 修改发票
     */
    @SaCheckPermission("gd:invoice:edit")
    @Log(title = "发票", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody ZInvoiceBo bo) {
        return toAjax(iZInvoiceService.updateByBo(bo));
    }

    /**
     * 删除发票
     *
     * @param numbers 主键串
     */
    @SaCheckPermission("gd:invoice:remove")
    @Log(title = "发票", businessType = BusinessType.DELETE)
    @DeleteMapping("/{numbers}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable String[] numbers) {
        return toAjax(iZInvoiceService.deleteWithValidByIds(Arrays.asList(numbers), true));
    }
}
